package com.liry.security.domain.vo;

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 返回结果对象
 *
 * @author 李瑞益
 * @since 2019/9/25
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData<T> implements Serializable {

    public static final String SUCCESS = "success";
    public static final String FAILED = "failed";

    private static final long serialVersionUID = -4304353934293881342L;

    /** 处理结果 */
    private boolean status;

    /** 信息 */
    private String message;

    /** 状态编码 */
    private String code;

    /** 数据对象 */
    private T data;

    public ResponseData(boolean status, T data) {
        this.status = status;
        this.data = data;
        this.code = status ? SUCCESS : FAILED;
    }

    public ResponseData(boolean status, T data, String message) {
        this.status = status;
        this.data = data;
        this.message = message;
        this.code = status ? SUCCESS : FAILED;
    }

    public ResponseData(Throwable e) {
        this.status = false;
        this.message = e.getMessage();
        this.code = FAILED;
    }

    public static ResponseData<Object> ok() {
        return new ResponseData<>(true, null);
    }


    public static ResponseData<Object> failed() {
        return new ResponseData<>(false, null);
    }

    public static ResponseData<Object> failed(Throwable e) {
        return new ResponseData<>(false, null, e.getMessage());
    }

    public static ResponseData<Object> failed(String message) {
        return new ResponseData<>(false, null, message);
    }
}
